from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

db = SQLAlchemy()

class FengshuiRecord(db.Model):
    """风水记录表"""
    __tablename__ = 'fengshui_record'
    
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True, comment='记录ID')
    fengshui_id = db.Column(db.String(32), unique=True, nullable=False, comment='风水ID')
    user_id = db.Column(db.BigInteger, db.ForeignKey('user.id'), nullable=True, comment='用户ID')
    house_type = db.Column(db.String(20), nullable=False, comment='房屋类型')
    direction = db.Column(db.String(20), nullable=False, comment='朝向')
    floor = db.Column(db.Integer, nullable=True, comment='楼层')
    area = db.Column(db.Numeric(10, 2), nullable=True, comment='面积')
    room_count = db.Column(db.Integer, nullable=True, comment='房间数')
    description = db.Column(db.Text, nullable=True, comment='房屋描述')
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now, comment='创建时间')
    update_time = db.Column(db.DateTime, nullable=False, default=datetime.now, onupdate=datetime.now, comment='更新时间')

    user = db.relationship('User', backref=db.backref('fengshui_records', lazy='dynamic'))

    def to_dict(self):
        return {
            'id': self.id,
            'fengshui_id': self.fengshui_id,
            'user_id': self.user_id,
            'house_type': self.house_type,
            'direction': self.direction,
            'floor': self.floor,
            'area': float(self.area) if self.area else None,
            'room_count': self.room_count,
            'description': self.description,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S')
        }


class FengshuiAnalysis(db.Model):
    """风水分析表"""
    __tablename__ = 'fengshui_analysis'
    
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True, comment='分析ID')
    fengshui_id = db.Column(db.String(32), db.ForeignKey('fengshui_record.fengshui_id'), nullable=False, comment='风水ID')
    basic_summary = db.Column(db.Text, nullable=True, comment='基础风水概述')
    direction_analysis = db.Column(db.Text, nullable=True, comment='朝向分析')
    layout_analysis = db.Column(db.Text, nullable=True, comment='布局分析')
    living_room_analysis = db.Column(db.Text, nullable=True, comment='客厅风水分析')
    bedroom_analysis = db.Column(db.Text, nullable=True, comment='卧室风水分析')
    kitchen_analysis = db.Column(db.Text, nullable=True, comment='厨房风水分析')
    bathroom_analysis = db.Column(db.Text, nullable=True, comment='卫生间风水分析')
    study_room_analysis = db.Column(db.Text, nullable=True, comment='书房风水分析')
    general_suggestion = db.Column(db.Text, nullable=True, comment='一般改善建议')
    specific_solution = db.Column(db.Text, nullable=True, comment='针对性改善方案')
    is_paid = db.Column(db.SmallInteger, nullable=False, default=0, comment='是否已付费查看')
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now, comment='创建时间')
    update_time = db.Column(db.DateTime, nullable=False, default=datetime.now, onupdate=datetime.now, comment='更新时间')

    fengshui_record = db.relationship('FengshuiRecord', backref=db.backref('analysis', uselist=False))

    def to_dict(self, include_paid_content=False):
        result = {
            'id': self.id,
            'fengshui_id': self.fengshui_id,
            'basic_summary': self.basic_summary,
            'direction_analysis': self.direction_analysis,
            'layout_analysis': self.layout_analysis,
            'is_paid': self.is_paid,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S')
        }
        
        if include_paid_content or self.is_paid:
            result.update({
                'living_room_analysis': self.living_room_analysis,
                'bedroom_analysis': self.bedroom_analysis,
                'kitchen_analysis': self.kitchen_analysis,
                'bathroom_analysis': self.bathroom_analysis,
                'study_room_analysis': self.study_room_analysis,
                'general_suggestion': self.general_suggestion,
                'specific_solution': self.specific_solution
            })
            
        return result


class FengshuiItem(db.Model):
    """风水物品表"""
    __tablename__ = 'fengshui_item'
    
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True, comment='物品ID')
    fengshui_id = db.Column(db.String(32), db.ForeignKey('fengshui_record.fengshui_id'), nullable=False, comment='风水ID')
    name = db.Column(db.String(50), nullable=False, comment='物品名称')
    description = db.Column(db.Text, nullable=True, comment='物品描述')
    position = db.Column(db.String(100), nullable=False, comment='摆放位置')
    effect = db.Column(db.Text, nullable=True, comment='预期效果')
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now, comment='创建时间')
    update_time = db.Column(db.DateTime, nullable=False, default=datetime.now, onupdate=datetime.now, comment='更新时间')

    fengshui_record = db.relationship('FengshuiRecord', backref=db.backref('items', lazy='dynamic'))

    def to_dict(self):
        return {
            'id': self.id,
            'fengshui_id': self.fengshui_id,
            'name': self.name,
            'description': self.description,
            'position': self.position,
            'effect': self.effect,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S')
        }
